package org.tigris.atlas.jmx.weblogic;

import java.util.Properties;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import weblogic.management.MBeanHome;
import weblogic.management.WebLogicMBean;
import weblogic.management.configuration.TargetMBean;

/**
 * Manages the configuration of a transactional JDBC datasource
 */
public class JDBCTxDatasourceConfiguration extends AbstractResourceConfiguation {	

	static final String ATTRIBUTE_POOL_NAME = "PoolName";
	static final String DATASOURCE_JNDI_NAME = "datasource.jndi.name";
	static final String JDBC_TRANSACTIONAL_DATASOURCE = "JDBCTxDataSource";
	
	private Log log = LogFactory.getLog(JDBCTxDatasourceConfiguration.class);	
	
	/**
	 * Creates a new instance of this configuration object
	 * @param mBeanName The name of the resource to manage
	 * @param properties The properties from which to manage the resorce
	 */
	public JDBCTxDatasourceConfiguration(String mBeanName, Properties properties) {
		super(mBeanName, properties);
	}
	
	/**
	 * @see org.tigris.atlas.jmx.weblogic.AbstractResourceConfiguation#getLog()
	 */
	protected Log getLog() {
		return log;
	}

	/**
	 * @see org.tigris.atlas.jmx.weblogic.AbstractResourceConfiguation#getMBeanType()
	 */
	protected String getMBeanType() {
		return JDBC_TRANSACTIONAL_DATASOURCE;
	}

	/**
	 * @see org.tigris.atlas.jmx.weblogic.AbstractResourceConfiguation#setMBeanAttributes(weblogic.management.configuration.TargetMBean[], weblogic.management.WebLogicMBean, weblogic.management.MBeanHome)
	 */
	protected void setMBeanAttributes(TargetMBean[] targetServers, WebLogicMBean mBean, MBeanHome home) {
		String jndiName = properties.getProperty(DATASOURCE_JNDI_NAME);
		String connectionPoolName = properties.getProperty(JDBCConnectionPoolConfiguation.CONNECTION_POOL_NAME);
		
		addAttribute(ATTRIBUTE_JNDI_NAME, jndiName, mBean);
		addAttribute(ATTRIBUTE_POOL_NAME, connectionPoolName, mBean);
		
		//set targets:
		targetToServers(targetServers, mBean);

	}

}
